﻿using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using WebSite.Core;
using WebSite.Data.NH.Infrastructure;
using WebSite.Model;

namespace WebSite.Data.NH.Repositories
{
    public interface IAdvRepository : IRepository<AdvInfo>
    {
        /// <summary>
        /// 通过Id获取
        /// </summary>
        /// <param name="advId">Id</param>
        /// <returns>AdvInfo</returns>
        AdvInfo GetAdv(int advId);

        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="adv">信息</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        bool AddAdv(AdvInfo adv);


        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="advId">Id</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        bool DeleteAdv(int advId);


        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="adv">信息</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        bool UpdateAdv(AdvInfo adv);


        /// <summary>
        /// 根据条件分页获得记录
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="orderByExpressions">排序字段</param>
        /// <returns>记录列表</returns>
        IList<AdvInfo> LoadAll(Expression<Func<AdvInfo, bool>> where,
                                                   int pageIndex,
                                                   int pageSize,
                                                   params IOrderByExpression<AdvInfo>[] orderByExpressions);


        /// <summary>
        /// 获取总数
        /// </summary>
        /// <param name="where">条件</param>
        /// <returns>记录总数</returns>
        int LoadCount(Expression<Func<AdvInfo, bool>> where);
    }

    public class AdvRepository : NhRepositoryBase<AdvInfo>, IAdvRepository
    {
        private readonly IUnitOfWork uow;

        public AdvRepository(ISession session, IUnitOfWork uow)
            : base(session)
        {
            this.uow = uow;
        }

        public AdvInfo GetAdv(int advId)
        {
            return GetById(advId);
        }

        public bool AddAdv(AdvInfo adv)
        {
            bool r = Add(adv);
            uow.Commit();
            return r;

        }

        public bool DeleteAdv(int advId)
        {
            var adv = GetById(advId);
            bool r = Delete(adv);
            uow.Commit();
            return r;
        }

        public bool UpdateAdv(AdvInfo adv)
        {
            bool r = Update(adv);
            uow.Commit();
            return r;
        }

        public IList<AdvInfo> LoadAll(Expression<Func<AdvInfo, bool>> where, int pageIndex, int pageSize, params IOrderByExpression<AdvInfo>[] orderByExpressions)
        {
            var query = GetMany(where, pageIndex, pageSize, orderByExpressions);
            return query.ToList();
        }

        public int LoadCount(Expression<Func<AdvInfo, bool>> where)
        {
            var query = GetMany(where);
            return query.Count();
        }
    }
}
